package com.mobilevikings.util;

import java.util.Hashtable;

public class Utils
{

    private static Hashtable htmlEntities;


    static
    {
        htmlEntities = new Hashtable();
        htmlEntities.put("&lt;", "<");
        htmlEntities.put("&gt;", ">");
        htmlEntities.put("&amp;", "&");
        htmlEntities.put("&quot;", "\"");
        htmlEntities.put("&agrave;", "à");
        htmlEntities.put("&Agrave;", "À");
        htmlEntities.put("&acirc;", "â");
        htmlEntities.put("&auml;", "ä");
        htmlEntities.put("&Auml;", "Ä");
        htmlEntities.put("&Acirc;", "Â");
        htmlEntities.put("&aring;", "å");
        htmlEntities.put("&Aring;", "Å");
        htmlEntities.put("&aelig;", "æ");
        htmlEntities.put("&AElig;", "Æ");
        htmlEntities.put("&ccedil;", "ç");
        htmlEntities.put("&Ccedil;", "Ç");
        htmlEntities.put("&eacute;", "é");
        htmlEntities.put("&Eacute;", "É");
        htmlEntities.put("&egrave;", "è");
        htmlEntities.put("&Egrave;", "È");
        htmlEntities.put("&ecirc;", "ê");
        htmlEntities.put("&Ecirc;", "Ê");
        htmlEntities.put("&euml;", "ë");
        htmlEntities.put("&Euml;", "Ë");
        htmlEntities.put("&iuml;", "ï");
        htmlEntities.put("&Iuml;", "Ï");
        htmlEntities.put("&ocirc;", "ô");
        htmlEntities.put("&Ocirc;", "Ô");
        htmlEntities.put("&ouml;", "ö");
        htmlEntities.put("&Ouml;", "Ö");
        htmlEntities.put("&oslash;", "ø");
        htmlEntities.put("&Oslash;", "Ø");
        htmlEntities.put("&szlig;", "ß");
        htmlEntities.put("&ugrave;", "ù");
        htmlEntities.put("&Ugrave;", "Ù");
        htmlEntities.put("&ucirc;", "û");
        htmlEntities.put("&Ucirc;", "Û");
        htmlEntities.put("&uuml;", "ü");
        htmlEntities.put("&Uuml;", "Ü");
        htmlEntities.put("&nbsp;", " ");
        htmlEntities.put("&copy;", "\u00a9");
        htmlEntities.put("&reg;", "\u00ae");
        htmlEntities.put("&euro;", "\u20a0");
    }

    public static String base64encode(String inText) {
        return Base64.base64Encode(inText.getBytes());
    }

    /**
     * Calculates an MD5-hash
     * @param inText the text of which an MD5-hash has to be calculated
     * @return a String of the MD5-hash
     */
    public static String md5(String inText)
    {
        MD5 md5 = new MD5();
        md5.Update(inText);
        byte[] outputBytes = md5.Final();
        StringBuffer hexString = new StringBuffer();
        for (int i = 0; i < outputBytes.length; i++)
        {
            String toAppend = Integer.toHexString(0xFF & outputBytes[i]);
            if (toAppend.length() == 1)
            {
                toAppend = '0' + toAppend;
            }
            hexString.append(toAppend);
        }
        return hexString.toString();
    }

    private static final char[] sHexChars =
    {
        '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
    };

    /**
     * Encodes a String, for example:
     * "hello there" will be "hello+there"
     * @param inString the String to be encoded
     * @return the encoded String
     */
    public static String urlEncode(String inString)
    {
        StringBuffer result = new StringBuffer();

        try
        {
            byte[] bytes = inString.getBytes("UTF-8");

            for (int i = 0; i < bytes.length; ++i)
            {
                char c = (char) bytes[i];
                if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '.' || c == '-' || c == '*' || c == '_')
                {
                    result.append(c);
                } else if (c == ' ')
                {
                    result.append('+');
                } else
                {
                    result.append('%');
                    result.append(sHexChars[(bytes[i] >>> 4) & 0xf]);
                    result.append(sHexChars[bytes[i] & 0xf]);
                }
            }
        } catch (Exception e)
        {
        }

        return result.toString();
    }

    /**
     * Removes the HTML-tags of a String
     * @param inText a text with HTML-tags (or XML)
     * @return the text, without the HTML-tags
     */
    public static String removeTags(String inText)
    {
        StringBuffer result = new StringBuffer();
        char[] chars = inText.toCharArray();
        boolean inTag = false;
        for (int i = 0; i < chars.length; i++)
        {
            if (chars[i] == '<')
            {
                if (chars[i + 1] == 'b' && chars[i + 2] == 'r')
                {
                    //speciaal geval <br />
                    result.append("\n");

                }
                inTag = true;
            } else if (chars[i] == '>')
            {
                inTag = false;
                continue;
            }
            if (!inTag)
            {
                result.append(chars[i]);
            }
        }
        return result.toString();
    }

    /**
     * Unescapes HTML, for example:
     * "&amp;" will be "&"
     * @param source
     * @return
     */
    public static String unescapeHTML(String inText) {
        return unescapeHTML(inText, 0);
    }

    public static String unescapeHTML(String inText, int start)
    {
        int i, j;

        i = inText.indexOf("&", start);
        if (i > -1)
        {
            j = inText.indexOf(";", i);
            if (j > i)
            {
                String entityToLookFor = inText.substring(i, j + 1);
                String value = (String) htmlEntities.get(entityToLookFor);
                if (value != null)
                {
                    inText = new StringBuffer().append(inText.substring(0, i)).append(value).append(inText.substring(j + 1)).toString();
                    return unescapeHTML(inText, i + 1); // recursive call
                }
            }
        }
        return inText;
    }
}